package com.sanyue.codingsettlement.controller;

import com.sanyue.codingsettlement.common.BaseResponse;
import com.sanyue.codingsettlement.common.DeleteRequest;
import com.sanyue.codingsettlement.common.ErrorCode;
import com.sanyue.codingsettlement.common.ResultUtils;
import com.sanyue.codingsettlement.exception.BusinessException;
import com.sanyue.codingsettlement.exception.ThrowUtils;
import com.sanyue.codingsettlement.model.dto.userteam.UserExitTeamRequest;
import com.sanyue.codingsettlement.model.dto.userteam.UserJoinTeamRequest;
import com.sanyue.codingsettlement.model.entity.UserTeam;
import com.sanyue.codingsettlement.model.entity.User;
import com.sanyue.codingsettlement.service.TeamService;
import com.sanyue.codingsettlement.service.UserTeamService;
import com.sanyue.codingsettlement.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户队伍关联表接口
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@RestController
@RequestMapping("/userTeam")
@Slf4j
public class UserTeamController {

    @Resource
    private UserTeamService userTeamService;

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    // region 增删改查

    /**
     * 用户加入队伍
     *
     * @param userJoinTeamRequest
     * @param request
     * @return
     */
    @PostMapping("/join")
    public BaseResponse<Long> userJoinTeam(@RequestBody UserJoinTeamRequest userJoinTeamRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userJoinTeamRequest == null, ErrorCode.PARAMS_ERROR);
        Long teamId = userJoinTeamRequest.getTeamId();
        ThrowUtils.throwIf(teamId == 0, ErrorCode.OPERATION_ERROR);
        User loginUser = userService.getLoginUser(request);
        // 写入数据库
        long userTeamId = userTeamService.userJoinTeam(userJoinTeamRequest, loginUser);
        ThrowUtils.throwIf(userTeamId <= 0, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(userTeamId);
    }

    /**
     * 删除用户队伍关联表
     *
     * @param userExitTeamRequest
     * @param request
     * @return
     */
    @PostMapping("/exit")
    public BaseResponse<Boolean> userExitTeam(@RequestBody UserExitTeamRequest userExitTeamRequest, HttpServletRequest request) {
        if (userExitTeamRequest == null || userExitTeamRequest.getTeamId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long teamId = userExitTeamRequest.getTeamId();
        boolean res = userTeamService.userExitTeam(teamId, loginUser);
        ThrowUtils.throwIf(!res, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }



    //感觉后面不需要

    // /**
    //  * 根据 id 获取用户队伍关联表（封装类）
    //  *
    //  * @param id
    //  * @return
    //  */
    // @GetMapping("/get/vo")
    // public BaseResponse<UserTeamVO> getUserTeamVOById(long id, HttpServletRequest request) {
    //     ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
    //     // 查询数据库
    //     UserTeam userTeam = userTeamService.getById(id);
    //     ThrowUtils.throwIf(userTeam == null, ErrorCode.NOT_FOUND_ERROR);
    //     // 获取封装类
    //     return ResultUtils.success(userTeamService.getUserTeamVO(userTeam, request));
    // }
    //
    // /**
    //  * 分页获取用户队伍关联表列表（仅管理员可用）
    //  *
    //  * @param userTeamQueryRequest
    //  * @return
    //  */
    // @PostMapping("/list/page")
    // @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    // public BaseResponse<Page<UserTeam>> listUserTeamByPage(@RequestBody UserTeamQueryRequest userTeamQueryRequest) {
    //     long current = userTeamQueryRequest.getCurrent();
    //     long size = userTeamQueryRequest.getPageSize();
    //     // 查询数据库
    //     Page<UserTeam> userTeamPage = userTeamService.page(new Page<>(current, size),
    //             userTeamService.getQueryWrapper(userTeamQueryRequest));
    //     return ResultUtils.success(userTeamPage);
    // }
    //
    // /**
    //  * 分页获取用户队伍关联表列表（封装类）
    //  *
    //  * @param userTeamQueryRequest
    //  * @param request
    //  * @return
    //  */
    // @PostMapping("/list/page/vo")
    // public BaseResponse<Page<UserTeamVO>> listUserTeamVOByPage(@RequestBody UserTeamQueryRequest userTeamQueryRequest,
    //                                                            HttpServletRequest request) {
    //     long current = userTeamQueryRequest.getCurrent();
    //     long size = userTeamQueryRequest.getPageSize();
    //     // 限制爬虫
    //     ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
    //     // 查询数据库
    //     Page<UserTeam> userTeamPage = userTeamService.page(new Page<>(current, size),
    //             userTeamService.getQueryWrapper(userTeamQueryRequest));
    //     // 获取封装类
    //     return ResultUtils.success(userTeamService.getUserTeamVOPage(userTeamPage, request));
    // }
    //
    // /**
    //  * 分页获取当前登录用户创建的用户队伍关联表列表
    //  *
    //  * @param userTeamQueryRequest
    //  * @param request
    //  * @return
    //  */
    // @PostMapping("/my/list/page/vo")
    // public BaseResponse<Page<UserTeamVO>> listMyUserTeamVOByPage(@RequestBody UserTeamQueryRequest userTeamQueryRequest,
    //                                                              HttpServletRequest request) {
    //     ThrowUtils.throwIf(userTeamQueryRequest == null, ErrorCode.PARAMS_ERROR);
    //     // 补充查询条件，只查询当前登录用户的数据
    //     User loginUser = userService.getLoginUser(request);
    //     userTeamQueryRequest.setUserId(loginUser.getId());
    //     long current = userTeamQueryRequest.getCurrent();
    //     long size = userTeamQueryRequest.getPageSize();
    //     // 限制爬虫
    //     ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
    //     // 查询数据库
    //     Page<UserTeam> userTeamPage = userTeamService.page(new Page<>(current, size),
    //             userTeamService.getQueryWrapper(userTeamQueryRequest));
    //     // 获取封装类
    //     return ResultUtils.success(userTeamService.getUserTeamVOPage(userTeamPage, request));
    // }
    //
    // /**
    //  * 编辑用户队伍关联表（给用户使用）
    //  *
    //  * @param userTeamEditRequest
    //  * @param request
    //  * @return
    //  */
    // @PostMapping("/edit")
    // public BaseResponse<Boolean> editUserTeam(@RequestBody UserTeamEditRequest userTeamEditRequest, HttpServletRequest request) {
    //     if (userTeamEditRequest == null || userTeamEditRequest.getId() <= 0) {
    //         throw new BusinessException(ErrorCode.PARAMS_ERROR);
    //     }
    //     // todo 在此处将实体类和 DTO 进行转换
    //     UserTeam userTeam = new UserTeam();
    //     BeanUtils.copyProperties(userTeamEditRequest, userTeam);
    //     // 数据校验
    //     userTeamService.validUserTeam(userTeam, false);
    //     User loginUser = userService.getLoginUser(request);
    //     // 判断是否存在
    //     long id = userTeamEditRequest.getId();
    //     UserTeam oldUserTeam = userTeamService.getById(id);
    //     ThrowUtils.throwIf(oldUserTeam == null, ErrorCode.NOT_FOUND_ERROR);
    //     // 仅本人或管理员可编辑
    //     if (!oldUserTeam.getUserId().equals(loginUser.getId()) && !userService.isAdmin(loginUser)) {
    //         throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
    //     }
    //     // 操作数据库
    //     boolean result = userTeamService.updateById(userTeam);
    //     ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
    //     return ResultUtils.success(true);
    // }

    // endregion
}
